package com.dingqb.mycs;

import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class HandlerActivity extends ActionBarActivity {
	private int delayMillis = 1000;
	private	int count = 0;
	private	TextView tv_count;
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			System.out.println("handleMessage==" + msg.what);
			tv_count.setText(msg.what+"");
			if(msg.what==20){
				tv_count.setText(msg.what+"次循环结束");
			}
	

		};

		@Override
		public void dispatchMessage(Message msg) {
			System.out.println("dispatchMessage==" + msg.what);
			super.dispatchMessage(msg);
		}

		@SuppressLint("NewApi")
		@Override
		public String getMessageName(Message message) {
			System.out.println("getMessageName==" + message.what);
			return super.getMessageName(message);
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_handler);
		tv_count=(TextView)findViewById(R.id.tv_count);
		// handler();
		final Timer timer = new Timer();
		timer.schedule(new TimerTask() {

			@Override
			public void run() {
				System.out.println("timer.schedule");
			}
		}, 1000);

		timer.schedule(new TimerTask() {

			@Override
			public void run() {
				System.out.println("延迟一秒之后 每两秒执行一次");
				count++;
				mHandler.sendEmptyMessage(count);
				if (count > 20) {

					timer.cancel();
				}

			}
		}, 1000, 2000);

	}

	private void handler() {
		mHandler.sendEmptyMessage(111);
		mHandler.post(new Runnable() {

			@Override
			public void run() {
				Message msg = new Message();
				msg.what = 1;
				mHandler.sendMessage(msg);

			}
		});
		mHandler.postDelayed(new Runnable() {

			@Override
			public void run() {

				Message msg = new Message();
				msg.what = 11;
				mHandler.sendMessage(msg);

			}
		}, delayMillis);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.handler, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();

			return true;
		}

		if (id == R.id.action_settings2) {
			Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
			return true;
		}
		if (id == R.id.action_settings3) {
			Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
			return true;
		}
		if (id == R.id.action_settings4) {
			Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
